package com.ease.model.util;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.ease.common.util.DateFormatUtil;
import com.ease.common.util.Detect;
import com.ease.model.type.DataType;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class DataTypeMarshalUtil extends Detect {

	public static Object unmarshal(DataType dataType, String value) throws Exception {
		if (StringUtils.isEmpty(value)) {
			return null;
		}
		switch (dataType) {
		case BOOLEAN: {
			return Boolean.parseBoolean(value);
		}
		case STRING: {
			return value;
		}
		case SINGLE_DICTIONARY: {
			return Short.parseShort(value);
		}
		case MULTIPLE_DICTIONARY: {
			return value;
		}
		case ENUMERABLE_MODEL: {
			return Long.parseLong(value);
		}
		case ENUMERABLE_REFERENCE: {
			return Long.parseLong(value);
		}
		case BYTE: {
			return Byte.parseByte(value);
		}
		case SHORT: {
			return Short.parseShort(value);
		}
		case INTEGER: {
			return Integer.parseInt(value);
		}
		case LONG: {
			return Long.parseLong(value);
		}
		case FLOAT: {
			return Float.parseFloat(value);
		}
		case DOUBLE: {
			return Double.parseDouble(value);
		}
		case DATE: {
			Date date = DateFormatUtil.toDate(value);
			return date != null ? date : DateFormatUtil.toDatetime(value);
		}
		case DATETIME: {
			return DateFormatUtil.toDatetime(value);
		}
		case TIME: {
			return DateFormatUtil.toDatetime(value);
		}
		case BLOB: {
			return value.getBytes();
		}
		case CLOB: {
			return value;
		}
		case RELATIONSHIP: {
			return Long.parseLong(value);
		}
		case LOCATIONRELATIONSHIP: {
			return Long.parseLong(value);
		}
		}
		return null;
	}

	public static String marshal(DataType dataType, Object value) {
		if (value == null) {
			return null;
		}
		switch (dataType) {
		case BOOLEAN: {
			return value.toString();
		}
		case STRING: {
			return value.toString();
		}
		case SINGLE_DICTIONARY: {
			return value.toString();
		}
		case MULTIPLE_DICTIONARY: {
			return value.toString();
		}
		case ENUMERABLE_MODEL: {
			return value.toString();
		}
		case ENUMERABLE_REFERENCE: {
			return value.toString();
		}
		case BYTE: {
			return value.toString();
		}
		case SHORT: {
			return value.toString();
		}
		case INTEGER: {
			return value.toString();
		}
		case LONG: {
			return value.toString();
		}
		case FLOAT: {
			return value.toString();
		}
		case DOUBLE: {
			return value.toString();
		}
		case DATE: {
			if (value instanceof Date) {
				return DateFormatUtil.toDateString((Date) value);
			}
		}
		case DATETIME: {
			if (value instanceof Date) {
				return DateFormatUtil.toDatetimeString((Date) value);
			}
		}
		case TIME: {
			return value.toString();
		}
		case BLOB: {
			return value.toString();
		}
		case CLOB: {
			return value.toString();
		}
		case RELATIONSHIP: {
			return value.toString();
		}
		case LOCATIONRELATIONSHIP: {
			return value.toString();
		}
		}
		return null;
	}

}
